home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_key.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  15.1 KB  |  621 lines

  1. /****************************************************************************/
  2. /*                         DATABOSS MODULE: DB_KEY.C                        */
  3. /****************************************************************************/
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #ifndef __TURBOC__
  8.    #include <graph.h>
  9. #endif
  10. #include <dos.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13. #include "db_lsc.h"
  14. #include "db_types.h"
  15. #include "db_conio.h"
  16. #include "db_dos.h"
  17. #include "db_key.h"
  18. #include "db_str.h"
  19.  
  20. /****************************  GLOBAL CONSTANTS  ****************************/
  21.  
  22. const mkey MLeftB   = {Enter, False};
  23. const mkey MRightB  = {Esc,   False};
  24. const mkey MBothB   = {F1,    True};
  25. const mkey MCenterB = {F1,    True};
  26. const mkey MCLeftB  = {F1,    True};
  27. const mkey MCRightB = {F1,    True};
  28. const mkey MAllB    = {F1,    True};
  29. const mkey MUp      = {UArr,  True};
  30. const mkey MDown    = {DArr,  True};
  31. const mkey MLeft    = {LArr,  True};
  32. const mkey MRight   = {RArr,  True};
  33.  
  34. /*
  35. const uchar Uxlat1[] = "ÇÜÉâÄàÅÇêëèïîìÄÅÉÆÆôÖòûùÿÖÜ¢£¥₧ƒáíóúÑÑ";
  36. const uchar Uxlat2[] = "ABΓπΣΣMTΦΘ∞φEH";
  37. const uchar Lxlat1[] = "çüéâäàåçêëèïîìäåéææôöòûùÿöü¢£¥₧ƒáíóúññ";
  38. const uchar Lxlat2[] = "αßΓπσσµτΦΘΩδ∞φε∩";
  39. */
  40. uchar Uxlate[]=
  41.     "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xA\xB\xC\xD\xE\xF"
  42.     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
  43.     " !""\x22""#$%&'()*+,-./0123456789:;<=>?"
  44.     "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[""\x5C""]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~""\x7F"
  45.     "ÇÜÉAÄAÅÇEEEIIIÄÅÉÆÆOÖOUUYÖÜ¢£¥₧ƒAIOUÑѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
  46.     "└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■""\xFF";
  47.  
  48. uchar Lxlate[]=
  49.     "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xA\xB\xC\xD\xE\xF"
  50.     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
  51.     " !""\x22""#$%&'()*+,-./0123456789:;<=>?"
  52.     "@abcdefghijklmnopqrstuvwxyz[""\x5C""]^_`abcdefghijklmnopqrstuvwxyz{|}~""\x7F"
  53.     "çüéâäàåçêëèïîìäåéææôöòûùÿöü¢£¥₧ƒáíóúññªº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
  54.     "└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■""\xFF";
  55.  
  56. const uchar IBMGrLines[] = "│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀";
  57. const uchar BasicLines[] = "|++++++\"+++++++++-++++++++=+++++++++++++|-||-";
  58.  
  59. /****************************  GLOBAL VARIABLES  ****************************/
  60.  
  61. bool       ehk;
  62. bool       _xkey;
  63. bool       _ckey;
  64. bool       _akey;
  65. bool       mousekey;
  66. bool       moused;
  67. background ssave;
  68. bool       scrrecurs;
  69. bool       resetdeltas;
  70. cditype    cdi;
  71. casemapfun casemapupch;
  72. kpressfunc kpressed;
  73. getkeyfunc getkey;
  74.  
  75.  
  76. /***************************  INTERNAL VARIABLES  ***************************/
  77.  
  78. static uchar   lastdirn;
  79. static byteptr keystats    = MK_FP(0x0040,0x0017);
  80. static wordptr mswintraddr = MK_FP(0x0000,0x00CC);
  81.  
  82. static bool initialized = False;
  83.  
  84. /*****************************  IMPLEMENTATION  *****************************/
  85.  
  86. word getvmode(void)
  87. {
  88.     union REGS  reg;
  89.    byte     videoMode, noRows, noCols, theAttr;
  90.    unsigned int font8x8 = 256;
  91.  
  92.     reg.h.ah = 15;
  93.  
  94. /*    swinterrupt(0x10,®,®); */
  95.  
  96.    int86(0x10, ®, ®);
  97.    videoMode = reg.h.al;                                             /* SN 3.5 */
  98.    if (!videoMode) {                                                 /* SN 3.5 */
  99.       getscreeninfo(&videoMode, &noCols, &noRows, &theAttr);         /* SN 3.5 */
  100.       if (noRows > 24) videoMode = font8x8;                          /* SN 3.5 */
  101.    }                                                                 /* SN 3.5 */
  102.     return (videoMode);                                               /* SN 3.5 */
  103. }
  104.  
  105. bool printer_ok(byte prt)
  106. {
  107.     union REGS regs;
  108.  
  109.     if ((prt >=1) && (prt <=3)) prt = 1;
  110.  
  111.     regs.h.ah = 2;
  112.     regs.x.dx = prt - 1;
  113.  
  114. /*    swinterrupt(0x17,®s,®s); */
  115.  
  116.    int86(0x17, ®s, ®s);
  117.     return ((bool) (((regs.h.ah & 0x10) == 0x10) && ((regs.h.ah & 0x20) != 0x20)));
  118. }
  119.  
  120. bool mintrloaded(void)
  121. {
  122.     union REGS regs;
  123.  
  124.     regs.x.ax = 0;
  125.  
  126. /*    swinterrupt(0x33,®s,®s); */
  127.  
  128.    int86(0x33, ®s, ®s);
  129.     return ((bool) (regs.x.ax != 0));
  130. }
  131.  
  132. bool mouseinstalled(void)
  133. {
  134.     return ((bool) ((*mswintraddr == 0) ? False : mintrloaded()));
  135. }
  136.  
  137. void setratio(int x, int y)
  138. {
  139.     union REGS regs;
  140.  
  141.     regs.x.ax = 0x0F;
  142.     regs.x.cx = x;
  143.     regs.x.dx = y;
  144.  
  145. /*    swinterrupt(0x33,®s,®s); */
  146.  
  147.    int86(0x33, ®s, ®s);
  148. }
  149.  
  150. void showmouse(void)
  151. {
  152.     union REGS regs;
  153.  
  154.     regs.x.ax = 1;
  155.  
  156. /* swinterrupt(0x33,®s,®s); */
  157.  
  158.    int86(0x33, ®s, ®s);
  159. }
  160.  
  161. void hidemouse(void)
  162. {
  163.     union REGS regs;
  164.  
  165.     regs.x.ax = 2;
  166.  
  167. /*    swinterrupt(0x33,®s,®s); */
  168.  
  169.    int86(0x33, ®s, ®s);
  170. }
  171.  
  172. void getmouse(mouseb *button, int *column, int *row)
  173. {
  174.     union REGS regs;
  175.  
  176.     regs.x.ax = 3;
  177.  
  178. /*    swinterrupt(0x33,®s,®s); */
  179.  
  180.    int86(0x33, ®s, ®s);
  181.     *column = ((regs.x.cx + 4) / 8) + 1;
  182.     *row    = ((regs.x.dx + 4) / 8) + 1;
  183.     *button = regs.x.bx & 0x07;
  184. }
  185.  
  186. void mgotoxy(int col, int row)
  187. {
  188.     union REGS regs;
  189.  
  190.     regs.x.ax = 4;
  191.     regs.x.cx = (col * 8) - 4 - 1;
  192.     regs.x.dx = (row * 8) - 4 - 1;
  193.  
  194. /*    swinterrupt(0x33,®s,®s); */
  195.  
  196.    int86(0x33, ®s, ®s);
  197. }
  198.  
  199. bool mousepressed(int mousep)
  200. {
  201.     union REGS regs;
  202.  
  203.     regs.x.ax = 5;
  204.     regs.x.bx = mousep;
  205.  
  206. /*    swinterrupt(0x33,®s,®s); */
  207.  
  208.    int86(0x33, ®s, ®s);
  209.     return ((bool) (regs.x.bx > 0));
  210. }
  211.  
  212. bool mousereleased(int mousep)
  213. {
  214.     union REGS regs;
  215.  
  216.     regs.x.ax = 6;
  217.     regs.x.bx = mousep;
  218.  
  219. /*    swinterrupt(0x33,®s,®s); */
  220.  
  221.    int86(0x33, ®s, ®s);
  222.     return ((bool) (regs.x.bx > 0));
  223. }
  224.  
  225. void mousehl(int left, int right)
  226. {
  227.     union REGS regs;
  228.  
  229.     regs.x.ax = 7;
  230.     regs.x.cx = (left * 8) - 1;
  231.     regs.x.dx = (right * 8) - 1;
  232.  
  233. /*    swinterrupt(0x33,®s,®s); */
  234.  
  235.    int86(0x33, ®s, ®s);
  236. }
  237.  
  238. void mousevl(int top, int bot)
  239. {
  240.     union REGS regs;
  241.  
  242.     regs.x.ax = 8;
  243.     regs.x.cx = (top * 8) - 1;
  244.     regs.x.dx = (bot * 8) - 1;
  245.  
  246. /*    swinterrupt(0x33,®s,®s); */
  247.  
  248.    int86(0x33, ®s, ®s);
  249. }
  250.  
  251. void mousechar(int ordchar)
  252. {
  253.     union REGS regs;
  254.  
  255.     regs.x.ax = 10;
  256.     regs.x.bx = 0;
  257.     regs.x.cx = 0x7700;
  258.     regs.x.dx = 0x0077 & ordchar;
  259.  
  260. /*    swinterrupt(0x33,®s,®s); */
  261.  
  262.    int86(0x33, ®s, ®s);
  263. }
  264.  
  265. uchar filterch(uchar inch, prtmode pmode)
  266. {
  267.     uchar fval;
  268.     int   p;
  269.  
  270.     if (inch < ' ') fval = ' ';
  271.     else {
  272.         if ((inch > 127) && (pmode == BasicChrs)) {
  273.             p = 0;
  274.             while ((IBMGrLines[p] != '\0') && (IBMGrLines[p] != inch)) p++;
  275.             fval = (IBMGrLines[p] != '\0') ? BasicLines[p] : ' ';
  276.         }
  277.         else fval = inch;
  278.     }
  279.     return (fval);
  280. }
  281.  
  282. strptr filterstr(strptr destination, strptr source, prtmode printMode)             /* SN 3.5 */
  283. {                                                                                  /* SN 3.5 */
  284.    strptr aStringPtr;                                                              /* SN 3.5 */
  285.    int    printPosition;                                                           /* SN 3.5 */
  286.                                            /* SN 3.5 */
  287.    aStringPtr = destination;                                                       /* SN 3.5 */
  288.    strcpy(destination, source);                                                    /* SN 3.5 */
  289.    if (printMode != AllChrs) {                                                     /* SN 3.5 */
  290.       while(*(aStringPtr)) {                                                                         /* SN 3.5 */
  291.      if (*aStringPtr < (char)0x20) *aStringPtr = (char)0x20;                   /* SN 3.5 */
  292.      else {                                                                    /* SN 3.5 */
  293.         if ((*aStringPtr > (char)0x7F) && (printMode == BasicChrs)) {           /* SN 3.5 */
  294.            printPosition = (int)strchr(IBMGrLines, *aStringPtr);                    /* SN 3.5 */
  295.            if (!printPosition) *aStringPtr = *(BasicLines + printPosition);    /* SN 3.5 */
  296.            else                *aStringPtr = (char)0x20;                       /* SN 3.5 */
  297.         }                                                                      /* SN 3.5 */
  298.      }                                                                         /* SN 3.5 */
  299.      aStringPtr++;
  300.       }                                                                              /* SN 3.5 */
  301.    }                                                                               /* SN 3.5 */
  302.    return(destination);                                                            /* SN 3.5 */
  303. }                                                                                  /* SN 3.5 */
  304.  
  305. uchar upperch(uchar ch)
  306. {
  307.     if (!_xkey) ch = Uxlate[ch];
  308.     return (ch);
  309. }
  310.  
  311. strptr upper(strptr sout, strptr strg)
  312. {
  313.     strptr sptr;
  314.  
  315.     _xkey = False;
  316.     sptr = sout;
  317.     while (*strg) *sptr++ = upperch(*strg++);
  318.     *sptr = '\0';
  319.     return (sout);
  320. }
  321.  
  322. uchar lowerch(uchar ch)
  323. {
  324.     if (!_xkey) ch = Lxlate[ch];
  325.     return (ch);
  326. }
  327.  
  328. strptr lower(strptr sout, strptr strg)
  329. {
  330.     strptr sptr;
  331.  
  332.     _xkey = False;
  333.     sptr = sout;
  334.     while (*strg) *sptr++ = lowerch(*strg++);
  335.     *sptr = '\0';
  336.     return (sout);
  337. }
  338.  
  339. bool detectk(byte key)
  340. {
  341.     union REGS regs;;
  342.  
  343.     regs.h.ah = 0x02;
  344.  
  345. /*    swinterrupt(0x16,®s,®s); */
  346.  
  347.    int86(0x16, ®s, ®s);
  348.     return ((bool) ((regs.h.al & key) != 0));
  349. }
  350.  
  351. bool enhdetectk(byte key)
  352. {
  353.     union REGS regs;
  354.  
  355.     regs.h.ah = 0x12;
  356.  
  357. /*    swinterrupt(0x16,®s,®s); */
  358.  
  359.    int86(0x16, ®s, ®s);
  360.     return ((bool) ((regs.h.ah & key) != 0));
  361. }
  362.  
  363. void setcaplock(bool on)
  364. {
  365.     if (on) *keystats |=  CapsLock;
  366.     else    *keystats &= ~CapsLock;
  367. }
  368.  
  369. void setnumlock(bool on)
  370. {
  371.     if (on) *keystats |=  NumLock;
  372.     else    *keystats &= ~NumLock;
  373. }
  374.  
  375. void setscrolllock(bool on)
  376. {
  377.     if (on) *keystats |=  ScrollLock;
  378.     else    *keystats &= ~ScrollLock;
  379. }
  380.  
  381. strptr keystat(strptr sout, byte mode)
  382. {
  383.     strcpy(sout,(detectk(CapsLock))   ? msg_CAPS   : "    ");         /* SN 3.5 */
  384.     strcat(sout,(detectk(NumLock))    ? msg_NUM    : "     ");        /* SN 3.5 */
  385.     strcat(sout,(detectk(ScrollLock)) ? msg_SCROLL : "      ");       /* SN 3.5 */
  386.     return (sout);
  387. }
  388.  
  389. void kflush(void)
  390. {
  391.     union REGS regs;
  392.  
  393.     if (moused) mgotoxy(MouseCH,MouseCV);
  394.     regs.h.ah = 0x0C;
  395.     regs.h.al = 0x06;
  396.     regs.h.dl = 0xFF;
  397.     intdos(®s,®s);
  398. }
  399.  
  400. bool internalkpressed(void)
  401. {
  402.     union REGS regs;
  403.     bool finished;
  404.     mouseb b;
  405.     int col,row;
  406.  
  407.     if (!scrrecurs && (ssave != NULL)) {
  408.         scrrecurs = True;
  409.         (*ssave)();
  410.         scrrecurs = False;
  411.     }
  412.     finished = False;
  413.     if (moused) {
  414.         getmouse((mouseb *)&b,&col,&row);
  415.         if ((b != NoB) || (col != MouseCH) || (row != MouseCV)) finished = True;
  416.     }
  417.     if (!finished) {
  418.         regs.h.ah = (byte) ((ehk) ? 0x11 : 0x01);
  419.  
  420. /*        swinterrupt(0x16,®s,®s); */
  421.  
  422. #ifdef __TURBOC__
  423.       int86(0x16, ®s, ®s);
  424.         finished = (bool) ((regs.x.flags & 0x0040) == 0);
  425. #else
  426.       finished = (bool) kbhit();
  427. #endif
  428.     }
  429.     if (finished) resetdeltas = True;
  430.     return (finished);
  431. }
  432.  
  433. uchar internalgetkey(void)
  434. {
  435.     union REGS regs;
  436.     bool finished,action;
  437.     uchar ch;
  438.     int col,row;
  439.     mouseb b,bt;
  440.     mkey mch;
  441.  
  442.     while (!kpressed());
  443.  
  444.     _xkey    = False;
  445.     mousekey = True;
  446.     finished = False;
  447.     action   = False;
  448.  
  449.     if (moused) {
  450.         do {
  451.             getmouse((mouseb *)&b,&col,&row);
  452.             if (b != NoB) {
  453.                 bt = b;
  454.                 delay(50);
  455.                 getmouse((mouseb *)&b,&col,&row);
  456.                 if (b == NoB) b = bt;
  457.             }
  458.             switch (b) {
  459.                 case LeftB   : mch = MLeftB; break;
  460.                 case RightB  : mch = MRightB; break;
  461.                 case BothB   : mch = MBothB; break;
  462.                 case CenterB : mch = MCenterB; break;
  463.                 case CLeftB  : mch = MCLeftB; break;
  464.                 case CRightB : mch = MCRightB; break;
  465.                 case AllB    : mch = MAllB; break;
  466.             }
  467.             if (b != NoB) {
  468.                 finished = True;
  469.                 while (b != NoB) getmouse((mouseb *)&b,&col,&row);
  470.             }
  471.             else {
  472.                 finished = True;
  473.                 if ((lastdirn == MRight.ch) || (lastdirn == MLeft.ch)) {
  474.                     if ((col - MouseCH) >= 1)      mch = MRight;
  475.                     else if ((MouseCH - col) >= 1) mch = MLeft;
  476.                     else if ((row - MouseCV) >= 1) mch = MDown;
  477.                     else if ((MouseCV - row) >= 1) mch = MUp;
  478.                     else finished = False;
  479.                 }
  480.                 else {
  481.                     if ((row - MouseCV) >= 1)      mch = MDown;
  482.                     else if ((MouseCV - row) >= 1) mch = MUp;
  483.                     else if ((col - MouseCH) >= 1) mch = MRight;
  484.                     else if ((MouseCH - col) >= 1) mch = MLeft;
  485.                     else finished = False;
  486.                 }
  487.             }
  488.             moused = False;
  489.             if (kpressed() || finished) action = True;
  490.             moused = True;
  491.         } while (!action);
  492.     }
  493.     if (moused && finished) {
  494.         mgotoxy(MouseCH,MouseCV);
  495.         ch = mch.ch;
  496.         _xkey = mch._xkey;
  497.         lastdirn = ch;
  498.     }
  499.     if (!finished) {
  500.         _ckey = detectk(CtrlKey);
  501.         _akey = detectk(AltKey);
  502.         mousekey = False;
  503.         regs.h.ah = (byte) ((ehk) ? 0x10 : 0x00);
  504.  
  505. /*        swinterrupt(0x16,®s,®s); */
  506.  
  507.       int86(0x16, ®s, ®s);
  508.         if ((regs.h.al == 0x00) || (regs.h.al == 0xE0)) {
  509.             ch = regs.h.ah | 0x80;
  510.             _xkey = True;
  511.         }
  512.         else
  513.             ch = regs.h.al;
  514.     }
  515.     return (ch);
  516. }
  517.  
  518. void delaykey(int time)
  519. {
  520.     int i;
  521.  
  522.     i = 1;
  523.     while (i++ < (time/100)) {
  524.         delay(100);
  525.         if (kpressed()) {
  526.             getkey();
  527.             break;
  528.         }
  529.     }
  530. }
  531.  
  532. bool enhkbd(void)
  533. {
  534.     union REGS regs;
  535.     byte b;
  536.  
  537.     regs.h.ah = 0x02;
  538.  
  539. /*    swinterrupt(0x16,®s,®s); */
  540.  
  541.    int86(0x16, ®s, ®s);
  542.     b = regs.h.al;
  543.     regs.x.ax = 0x1200 + (b ^ 0xFF);
  544.  
  545. /*    swinterrupt(0x16,®s,®s); */
  546.  
  547.    int86(0x16, ®s, ®s);
  548.     return ((bool) (regs.h.al == b));
  549. }
  550.  
  551. void chkforkswitch(void)
  552. {
  553.     int i;
  554.     string ts;
  555.  
  556.     for (i = 1; i < paramcount(); i++) {
  557.         strcpy(ts,paramstr(i));
  558.         if ((*ts == '-') || (*ts == '/')) {
  559.             strdelete(ts,0,1);
  560.             if (strcmpi(ts,"K") == 0) ehk = False;
  561.         }
  562.     }
  563. }
  564.  
  565. void initcdi(void)
  566. {
  567.     union REGS regs;
  568.     struct SREGS sregs;
  569.     ptr p;
  570.     int ch_i;
  571.  
  572.     p = &cdi;
  573.     regs.x.ax = 0x3800;
  574.     regs.x.dx = FP_OFF(p);
  575.     sregs.ds = FP_SEG(p);
  576.     intdosx(®s,®s,&sregs);
  577.  
  578. /*************************************************************************
  579.     The case map function is provided as a feature of Microsoft DOS. The
  580.     address of the case map function is returned as part of the Country
  581.     Dependant Information block vai a call to Int 21 function 0x38.  This
  582.     case map function needs the character to be uppercased to be put into
  583.     the "AL" register, a far call to be placed to the "case_map" address
  584.     after which the upper cased character is returned in th "AL" register.
  585.     The mapping function is only provided for characters >= \x80.  If a
  586.     character does not have a corresponding upper case character then the
  587.     contents of "AL" are unchanged.
  588.  
  589.     Use of the "casemapupch" function is dependant upon the implementation
  590.     of the compiler.  It assumes that when the "C" compilers detect a
  591.     "char" function with one "char" parameter it will load the parameter
  592.     into the "AL" register and will return the contents of the "AL" register
  593.     as the function result.
  594. *************************************************************************/
  595.     casemapupch = cdi.case_map;
  596.     for (ch_i = 0x80; ch_i <= 0xFF; ch_i++) Uxlate[ch_i] = casemapupch((char)ch_i);
  597. }
  598.  
  599. void db_key_init(void)
  600. {
  601.     if (!initialized) {
  602.       kpressed = internalkpressed;
  603.       getkey   = internalgetkey;
  604.         initialized = True;
  605.         _xkey = _ckey = _akey = False;
  606.         scrrecurs = False;
  607.         ssave = NULL;
  608.         ehk = enhkbd();
  609.         chkforkswitch();
  610.         mousekey = False;
  611.         moused = mouseinstalled();
  612.         if (moused) {
  613.             setratio(40,40);
  614.             mgotoxy(MouseCH,MouseCV);
  615.         }
  616.         initcdi();
  617.     }
  618. }
  619.  
  620. /*****************************  END OF DB_KEY.C  ****************************/
  621.